import { raf, cancelRaf } from '../utils/dom/raf'
import { getScrollTop, setScrollTop } from '../utils/dom/scroll'
var scrollLeftRafId
export function scrollLeftTo(scroller, to, duration) {
  cancelRaf(scrollLeftRafId)
  var count = 0
  var from = scroller.scrollLeft
  var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16)

  function animate() {
    scroller.scrollLeft += (to - from) / frames

    if (++count < frames) {
      scrollLeftRafId = raf(animate)
    }
  }

  animate()
}
export function scrollTopTo(scroller, to, duration, callback) {
  var current = getScrollTop(scroller)
  var isDown = current < to
  var frames = duration === 0 ? 1 : Math.round(duration * 1000 / 16)
  var step = (to - current) / frames

  function animate() {
    current += step

    if (isDown && current > to || !isDown && current < to) {
      current = to
    }

    setScrollTop(scroller, current)

    if (isDown && current < to || !isDown && current > to) {
      raf(animate)
    } else if (callback) {
      raf(callback)
    }
  }

  animate()
}
